package com.atguigu.spzx.product.service.Impl;

import com.alibaba.fastjson.JSON;
import com.atguigu.spyx.model.common.com.atguigu.spyx.model.entity.h5.OrderItem;
import com.atguigu.spyx.model.common.com.atguigu.spyx.model.entity.product.Product;
import com.atguigu.spyx.model.common.com.atguigu.spyx.model.entity.product.ProductDetails;
import com.atguigu.spyx.model.common.com.atguigu.spyx.model.entity.product.ProductSku;
import com.atguigu.spyx.model.dto.h5.ProductSkuDto;
import com.atguigu.spyx.model.vo.h5.ProductSkuVo;
import com.atguigu.spyx.model.vo.h5.SpecValueVo;
import com.atguigu.spzx.product.mapper.ProductDetailsMapper;
import com.atguigu.spzx.product.mapper.ProductMapper;
import com.atguigu.spzx.product.mapper.ProductSkuMapper;
import com.atguigu.spzx.product.service.ProductSkuService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import jakarta.annotation.Resource;
import org.apache.commons.lang3.StringUtils;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author LT
 * @create 2023-11-18-16:39
 */
@Service
public class ProductSkuServiceImpl implements ProductSkuService {

    @Resource
    ProductSkuMapper productSkuMapper;
    @Resource
    ProductMapper productMapper;
    @Resource
    ProductDetailsMapper productDetailsMapper;

    @Override
    public PageInfo<ProductSku> page(Integer page, Integer limit, ProductSkuDto productSkuDto) {
        PageHelper.startPage(page,limit);
        List<ProductSku> productSkuList = productSkuMapper.page(productSkuDto);
        PageInfo pageInfo = new PageInfo<>(productSkuList);

        return pageInfo;
    }
    @Cacheable(value = "product:sku" , key = "#skuId")
    @Override
    public ProductSkuVo findSkuById(Long skuId) {
        //1、查询sku的详情数据
        ProductSku productSku = productSkuMapper.findById(skuId);
        //2、查询product的详情数据
        Product product = productMapper.findById(productSku.getProductId());
        //3、查询product_details的详情图数据
        ProductDetails productDetails = productDetailsMapper.findById(productSku.getProductId());
        //4、需要查询product下所有的sku列表 组装下面的数据
        List<ProductSku> productSkuList = productSkuMapper.findByProductId(product.getId());
        ProductSkuVo productSkuVo = new ProductSkuVo();
        productSkuVo.setProductSku(productSku);
        productSkuVo.setProduct(product);
        //轮播图
        if(!StringUtils.isEmpty(product.getSliderUrls())){
            //将轮播图字符串 分隔转为List集合
            productSkuVo.setSliderUrlList(Arrays.asList(product.getSliderUrls().split(",")));
        }
        //详情图
        String imageUrls = productDetails.getImageUrls();
        if(!StringUtils.isEmpty(imageUrls)){
            productSkuVo.setDetailsImageUrlList(Arrays.asList(imageUrls.split(",")));
        }
        //product的所有规格参数值
        String specValue = product.getSpecValue();
        if(!StringUtils.isEmpty(specValue)){
            //[{"key":"颜色","valueList":["白色"]},{"key":"内存","valueList":["8G","12G"]}]
            List<SpecValueVo> specValueVos = JSON.parseArray(specValue, SpecValueVo.class);
            productSkuVo.setSpecValueList(specValueVos);
        }
        //spu下所有的sku的规格参数值 映射skuId的Map
        Map<String, Long> map = productSkuList.stream()
                .collect(Collectors  //将productSkuList中每个对象的 skuSpec值作为键  id值作为值 存到map中
                        .toMap(ProductSku::getSkuSpec, ProductSku::getId));

        productSkuVo.setSkuSpecValueMap(map);
        return productSkuVo;
    }

    @Override
    public ProductSku findById(Long skuId) {
        return productSkuMapper.findById(skuId);
    }

    @Override
    public void updateSalesAndStock(List<OrderItem> orderItems) {
        orderItems.forEach(orderItem -> {
            updateSalesAndStockBySkuId(orderItem);
        });

    }

    @CacheEvict(value = "product:sku" , key = "#orderItem.skuId")
    public void updateSalesAndStockBySkuId(OrderItem orderItem) {
        productSkuMapper.updateSalesAndStock(orderItem.getSkuId(),orderItem.getSkuNum());
    }

}
